define(function (require) {

    require('../../../c/base/index');

    require('../../../c/nav/index');

    var shopTable;
    var firstTime = true;

    var Config = {
        init: function () {
            //初始化菜单选中效果
            $(".fa-circle-o").each(function () {
                if ($(this).parents("a").text() == "push失败列表") {
                    $(this).parents("a").parents("li").addClass("active");
                }
            });
            this.initShopTable();
            this.doSearch();
        },
        /**
         * 查询
         */
        doSearch: function () {
            $('#shop_table').DataTable().ajax.reload();
        },
        /**
         * 初始化table
         */
        initShopTable: function () {
            var self = this;
            shopTable = $("#shop_table").dataTable({
                "oLanguage": CRM.pageConfig,
                "sScrollX": "100%", //表格的宽度
                "sScrollXInner": "100%", //表格的内容宽度
                "bScrollCollapse": true, //当显示的数据不足以支撑表格的默认的高度时，依然显示纵向的滚动条。(默认是false)
                "bPaginate": true, //是否显示分页
                "bLengthChange": false, //每页显示的记录数
                "bFilter": false, //搜索栏
                "bSort": false, //是否支持排序功能
                "bInfo": false, //显示表格信息
                "bAutoWidth": false, //自适应宽度
                "bServerSide": true, //开启服务端处理
                "sAjaxSource": CRM.getOuterUrl('/trade/crm/push/queryPushStatistics'),
                "fnInitComplete": function () {
                    this.fnAdjustColumnSizing(true);
                },
                "fnServerData": function (sSource, aoData, fnCallback) {
                    if (firstTime) {
                        firstTime = false;
                        return;
                    }
                    var _url = CRM.getOuterUrl('/trade/crm/push/queryPushStatistics');
                    var _date = {};
                    _date['pageNo'] = aoData[3].value / aoData[4].value + 1;
                    _date['pageSize'] = aoData[4].value;
                    $.ajax({
                        "type": 'GET',
                        "url": _url,
                        "dataType": "json",
                        contentType: "application/json",
                        xhrFields: {
                            withCredentials: true
                        },
                        "data": _date,
                        contentType: "application/json",
                        "success": function (req) {
                            if (req.responseCode && req.status) {
                                var resultData = CRM.getDataTabelDtaObject();
                                if (req.entry) {
                                    resultData.iTotalDisplayRecords = 99999999;
                                    resultData.iTotalRecords = 99999999;
                                    $.each(req.entry, function (key, entry) {
                                        //将时间戳转换为YYYY/MM/DD hh:ii:ss
                                        var date = new Date(entry.gmtModified);
                                        resultData.aaData.push([
                                            CRM.formatDate(date),
                                            entry.failCnt,
                                            CRM.htmlEncode(entry.phoneOs),
                                            CRM.htmlEncode(entry.systemVersion),
                                            CRM.htmlEncode(entry.phoneBrand),
                                            CRM.htmlEncode(entry.phoneName),
                                            CRM.htmlEncode(entry.appVersion),
                                            entry.mobile,
                                            entry.shopId,
                                            CRM.htmlEncode(entry.shopXiaoer),
                                            entry.xiaoerMobile,
                                            CRM.htmlEncode(entry.city),
                                            "未处理",
                                            entry
                                        ]);
                                    });
                                }
                            } else {
                                $("#msg_dialog_content").text(req.message);
                                $('#msg_dialog').modal('show');
                            }
                            fnCallback(resultData);
                        },
                        error: function (e) {
                            //请求出错处理
                            $("#msg_dialog_content").text("获取push失败列表,请求服务异常！");
                            $('#msg_dialog').modal('show');
                        }
                    });
                },
                "aaSorting": [
                    [0, "desc"]
                ], //给列表排序 ，第一个参数表示数组 (由0开始)。1 表示Browser列。第二个参数为 desc或是asc
                "aoColumns": [{
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true,
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true,
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true,
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true,
                    "mRender": function (data) {
                        var html = '<div class="btn btn-default" onclick=\"clearFail(' + data.mobile + ')\">解决</div>';
                        return html;
                    }
                }

                ], //列设置，表有几列，数组就有几项
                "bStateSave": true, //保存状态到cookie *************** 很重要 ， 当搜索的时候页面一刷新会导致搜索的消失。使用这个属性就可避免了
                "sPaginationType": "simple" //分页，一共两种样式，full_numbers和two_button(默认)

            });
        },
        /**
         * 清除push失败记录
         */
        clearFail: function (mobile) {
            var self = this;
            var _date = {};
            _date['mobile'] = mobile;
            CRM.request({
                "type": 'GET',
                "url": CRM.getOuterUrl('/trade/crm/push/clearFailedPush'),
                "dataType": "json",
                contentType: "application/json",
                "data": _date,
                "success": function (req) {
                    if (req.status) {
                        self.doSearch();
                    } else {
                        CRM.showDialog(req.message);
                    }
                }
            });
        }
    }
    window.clearFail = $.proxy(Config.clearFail, Config);
    Config.init();
});
